package cn.iktz.mather.know.web.controller;

import java.util.Base64;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.iktz.mather.know.web.utils.HttpUtils;
import net.sf.json.JSONObject;

@Controller
public class SearchController {

	// private static final Log SEARCH_LOGGER =
	// LogFactory.getLog("SEARCH_LOGGER");

	@RequestMapping("/s")
	public String search(@RequestParam(required = true) String w, Integer p, Integer r, HttpServletRequest request) {

		try {
			if(p==null){
				p=1;
			}
			String json = HttpUtils.getContentAsString("http://localhost:8080/s?w=" + w + "&num=" + 10+"&p="+p);
			JSONObject object = JSONObject.fromObject(json);
			request.setAttribute("data", object);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "ask_search_list";
	}

	@RequestMapping("/")
	public String index(HttpServletRequest request) {
		try {
			String json = HttpUtils.getContentAsString("http://localhost:8080/s?w=");
			JSONObject object = JSONObject.fromObject(json);
			request.setAttribute("data", object);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "ask_search_list";
	}
	
	@RequestMapping("/all")
	public String all(HttpServletRequest request) {
		try {
			String json = HttpUtils.getContentAsString("http://localhost:8080/all");
			JSONObject object = JSONObject.fromObject(json);
			request.setAttribute("data", object);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "ask_search_list";
	}
//	@RequestMapping("/")
//	public String index(Integer num, HttpServletRequest request) {
//		try {
//			String json = HttpUtils.getContentAsString("http://localhost:8080/list?num=10");
//			JSONObject object = JSONObject.fromObject(json);
//			request.setAttribute("list", object);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return "ask_search_list";
//	}

	@RequestMapping("/list")
	public String list(Integer num, HttpServletRequest request) {
		try {
			String json = HttpUtils.getContentAsString("http://localhost:8080/list?num=10");
			JSONObject object = JSONObject.fromObject(json);
			request.setAttribute("list", object);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "ask_index";
	}

	@RequestMapping("/ask/detail")
	public String askDetail() {
		return "ask_detail";
	}

	@RequestMapping(value = "/ask/detail/{id}")
	public String getUrlParam(@PathVariable("id") String id, HttpServletRequest request) {
		try {
			// String json = IOUtils.toString(new
			// FileReader("C:\\dev\\Document.json"));
			String json = HttpUtils.getContentAsString("http://localhost:8080/ask/detail/" + id);
			JSONObject object = JSONObject.fromObject(json);
			request.setAttribute("qa", object.getJSONArray("items").get(0));
			String recJson = HttpUtils.getContentAsString("http://localhost:8080/ask/rec/" + id + "?num=3");
			JSONObject object1 = JSONObject.fromObject(recJson);
			request.setAttribute("rec", object1.getJSONArray("items"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "ask_detail";
	}

	@RequestMapping("/click")
	public void redict(String u, HttpServletRequest request, HttpServletResponse response) {
		try {
			response.sendRedirect(new String(Base64.getDecoder().decode(u)));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String createUUID() {
		return UUID.randomUUID().toString().replaceAll("-", "");
	}
}
